package cn.itcast.bos.web.action.base;

import cn.itcast.bos.service.base.StandardService;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>  {
	
	private static final long serialVersionUID = 1L;
	private Standard standard = new Standard();
	@Autowired
	private StandardService standardService;
	@Override
	public Standard getModel() {
		
		return standard;
	}
	//接受页面传输的参数
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value ="standard_save", results = { @Result(name = "success",type="redirect", location = "./pages/base/standard.html") })
	public String saveStandard(){
		standardService.save(standard);
		return SUCCESS;
	}
	@Action(value="standard_pageQuery",results = {@Result(name="success",type="json")})
	public String  pageQueryStandard(){
		Pageable pageable = new PageRequest(page-1,rows);
		Page<Standard> pageData= standardService.pageQuery(pageable);
		//将pageData封装成指定格式转化成json,返回页面
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total",pageData.getNumberOfElements());
		map.put("rows",pageData.getContent());
		//将map压如 值栈
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;

	}
	//删除的操作,不是真的删除,只是修改状态,不在页面显示
	@Action(value = "standard_del",results = {@Result(name="success",type="redirect",location ="./pages/base/standard.html" )})
	public String delStandard(){
		System.out.println(ServletActionContext.getRequest().getParameter("id"));
		ServletActionContext.getRequest().getParameter("id");
		return SUCCESS;
	}

	//查询所有的取派标准,回显到添加快递员的页面
	@Action(value = "standard_findAll",results = {@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> standards = standardService.findAll();
		//将结果压如栈顶
		System.out.println(standards);

		ActionContext.getContext().getValueStack().push(standards);
		return SUCCESS;
	}


}
